package com.talkweb.osmharmony.views.overlay.advancedpolyline;


import com.talkweb.osmharmony.views.overlay.PaintList;
import ohos.agp.render.LinearShader;
import ohos.agp.render.Paint;
import ohos.agp.render.Shader;
import ohos.agp.utils.Color;
import ohos.agp.utils.Point;

/**
 * A real {@link PaintList} with potentially different colors for each segment, and linear gradients
 *
 * @author Fabrice Fontaine
 * @since 6.2.0
 */
public class PolychromaticPaintList implements PaintList {

    private final Paint mPaint;
    private final ColorMapping mColorMapping;
    private final boolean mUseGradient;

    /**
     * @param pPaint        Basis Paint
     * @param pColorMapping from where we get the color to use for each geo segment
     * @param pUseGradient  should we use a gradient from this segment's color to the next segment's
     */
    public PolychromaticPaintList(final Paint pPaint, final ColorMapping pColorMapping, final boolean pUseGradient) {
        mPaint = pPaint;
        mColorMapping = pColorMapping;
        mUseGradient = pUseGradient;
    }

    @Override
    public Paint getPaint() {
        return null;
    }

    @Override
    public Paint getPaint(final int pIndex, final float pX0, final float pY0, final float pX1, final float pY1) {
        final int startColor = mColorMapping.getColorForIndex(pIndex);
        if (mUseGradient) {
            final int endColor = mColorMapping.getColorForIndex(pIndex + 1);
            if (startColor != endColor) {
                Point[] points = new Point[]{};
                points[0] = new Point(pX0,pY0);
                points[1] = new Point(pX1,pY1);
                Color[] colors = new Color[]{};
                colors[0] = new Color(startColor);
                colors[1] = new Color(endColor);
                final Shader shader = new LinearShader(points, null,colors, Shader.TileMode.CLAMP_TILEMODE);
                mPaint.setShader(shader, Paint.ShaderType.LINEAR_SHADER);
                return mPaint;
            }
            mPaint.setShader(null,null);
        }
        mPaint.setColor(new Color(startColor));
        return mPaint;
    }
}